package gen.tools.envSquad;

import gen.tools.Widgets;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MES extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public MES() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		final String cid = request.getParameter("cid").replace("&nbsp;", "");
		final String cip = request.getParameter("cip").replace("&nbsp;", "");
		final int cport = wid.getParNumberFromRequest( request.getParameter("cport") );
		final String cpwd = request.getParameter("cpwd").replace("&nbsp;", "");
		final String empId = request.getParameter("empId");
		final String squadId = request.getParameter("id");
		final String oper = request.getParameter("oper");
		final String bssId = "fOrder";
		final String subsysId = request.getParameter("subsysId");
		final String envId = request.getParameter("envId");
		Connection conn = null;
		PreparedStatement ps = null;
		String res = "NOTOK";
		try {
			conn = wid.getConn();
			
			if ("add".equalsIgnoreCase(oper)) {
				final String sql = "INSERT INTO mpsquadenv (bssid,subsysid,envid,squadid,creator,createdDateTime,cip,cid,cport,cpwd) VALUES(?,?,?,?,?,?,?,?,?,?)";
				ps = conn.prepareStatement(sql);
				ps.setString(1, bssId);
				ps.setString(2, subsysId);
				ps.setString(3, envId);
				ps.setString(4, squadId);
				ps.setString(5, empId);
				ps.setTimestamp(6, wid.cDT());
				ps.setString(7, cip);
				ps.setString(8, cid);
				ps.setInt(9, cport);
				ps.setString(10, cpwd);
				ps.execute();
			} else if ("edit".equalsIgnoreCase(oper)) {
				final String sql = "UPDATE mpsquadenv SET cip=?,cid=?,cport=?,cpwd=? WHERE bssid=? AND subsysid=? AND envid=? AND squadid=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, cip);
				ps.setString(2, cid);
				ps.setInt(3, cport);
				ps.setString(4, cpwd);
				ps.setString(5, bssId);
				ps.setString(6, subsysId);
				ps.setString(7, envId);
				ps.setString(8, squadId);
				ps.execute();
			} else {
				final String sql = "DELETE FROM mpsquadenv WHERE bssid=? AND subsysid=? AND envid=? AND squadid=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1, bssId);
				ps.setString(2, subsysId);
				ps.setString(3, envId);
				ps.setString(4, squadId);
				ps.execute();
			}
			res = "OK";
			
		} catch(Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if ( ps !=null ) { ps.close(); }
				if ( conn !=null ) { conn.close(); }
			} catch (Exception e) { e.printStackTrace(); }
		}
		
		response.getWriter().write(res);
	}

}
